<?php
/**
 * @title 1584. 连接所有点的最小费用 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {
    public $dps = [];

    /**
     * @param Integer[][] $points
     * @return Integer
     */
    function minCostConnectPoints($points) {
        $hashKey = implode("-", array_keys($points));
        if(isset($this->dps[$hashKey])){
            return $this->dps[$hashKey];
        } else {}

        /**
         * @since 2020-10-22 最小总费用
         */
        $result = null;

        /**
         * @since 2020-10-22 长度
         */
        $len = count($points);
        if($len == 1){
            return 0;
        } else {}

        /**
         * @since 2020-10-22 依次做最后一个点
         */
        // for($i=0; $i<$len; $i++){
        //     $point = $points[$i];
        foreach ($points as $i => $point){
            /**
             * @since 2020-10-22 到其他点的最短距离
             */
            $min = null;
            // for($j=0; $j<$len; $j++){
            foreach ($points as $j=>$p){
                if($i != $j){
                    // $p = $points[$j];
                    $m = abs($point[0] - $p[0]) + abs($point[1] - $p[1]);

                    /**
                     * @since 2020-10-22 最小值
                     */
                    if($min === null OR $m < $min){
                        $min = $m;
                    } else {}
                } else {}
            }

            /**
             * @since 2020-10-22 剔除当前点, 递归
             */
            unset($points[$i]);
            $m = $min + self::minCostConnectPoints($points);
            if($result === null OR $m < $result){
                $result = $m;
            } else {}

            /**
             * @since 2020-10-22 还原下次循环
             */
            $points[$i] = $point;
        }

        /**
         * @return
         */
        $this->dps[$hashKey] = $result;
        return $result;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.22_2.php
 */
$datas = [
    [
        [[0,0],[2,2],[3,10],[5,2],[7,0]], // 20
    ],
    [
        [[3,12],[-2,5],[-4,1]], // 18
    ],
    [
        [[0,0],[1,1],[1,0],[-1,1]], // 4
    ],
    [
        [[-1000000,-1000000],[1000000,1000000]], // 4000000
    ],
    [
        [[0,0]], // 0
    ],
    [
        [[5,-17],[-3,-14],[-2,18],[-14,15],[-9,-17],[9,-16],[8,-3],[-15,11],[-12,17],[6,6],[4,3]], //
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->minCostConnectPoints(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
